--
-- Author: shunguo.chen
-- Date: 2018-04-12 18:12:53
--
-- PvpLoadingRender
--

local AvatarIconNode = require("app.widget.AvatarIconNode")

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.PvpLoadingNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

-- data = {baseInfo=UserBaseInfo, progress=}
function ClassRef:init(data)
    self.super.init(self, data)

    self._avatarIcon = AvatarIconNode.new(self._ui.Panel, "icon")
    self._numText = self:findChild("Panel/num")

    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        local baseInfo = self._data.baseInfo
        self._isMe = (baseInfo.uid == UD:getUid())
        self._isRobot = (baseInfo.uid < 0)
        self._avatarIcon:update(baseInfo)
        self._ui.name:setString(baseInfo.name)
        self._ui.type:setString(baseInfo:getDuty())
        self:setProgress(self._data.progress)
    end
end

-- 我或者机器人 使用自己的loading进度
function ClassRef:isUseMy()
    return self._isMe or self._isRobot
end

function ClassRef:setIsMy(b)
    self._ui.bg_blue:setVisible(b)
    self._ui.bg_red:setVisible(not b)
end

function ClassRef:addProgress(pg)
    local curPg = self._data.progress
    if (curPg + pg) < 100 then
        self:setProgress(curPg + pg)
    end
end

function ClassRef:setProgress(pg)
    self._data.progress = pg
    self._numText:setString(string.format("%s%%", pg))
end

return ClassRef
